<template>
  <view>
    
  </view>
</template>

<script>
  export default {
    name:"dialogue",
    data() {
      return {
        notfound:0,
        text:'',
        same:0
      };
    },
    methods:{
      timer(aid) {
      let  dhtime = setTimeout(()=>{
          uni.request({
            url: getApp().globalData.apiDomain + 'ai/getSSEContent',
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded', //自定义请求头信息
              'token': uni.getStorageSync('wbyun_token'),
              'wbapp':'weixin_mp_xbgy'
            },
            data: {
              'aid':aid
            },
            dataType: 'json',
            success: res => {
                console.log(res);
                if(res.data.code == 200) {
                  if(res.data.datas.status == 300) {
                      if(this.text ==  res.data.datas.content) {
                        this.same+=1 
                        console.log(this.same);
                        if(this.same >= 8) {
                          console.log('清除');
                          clearTimeout(dhtime)
                          this.$emit("backHome",'【小猪】：'+res.data.datas.content,aid,'ok')
                          this.same = 0
                          this.text = ''
                          return
                        }else {
                          console.log('继续');
                          this.timer(aid)
                          this.$emit("backHome",'【小猪】：'+res.data.datas.content,aid)
                        }
                      }else {
                        this.timer(aid)
                        this.$emit("backHome",'【小猪】：'+res.data.datas.content,aid)
                      }
                      console.log(this.same);
                      this.text = res.data.datas.content
                  }else if(res.data.datas.status == 100) {
                    this.notfound+=1
                    if(this.notfound >= 8) {
                      this.$emit("backHome",'【小猪】：'+'当前聊天人数过多，请稍后再试。',aid,'error')
                      this.notfound = 0
                      return
                    }
                      this.timer(aid)
                  }else if(res.data.datas.status == 200) {
                      this.$emit("backHome",'【小猪】：'+res.data.datas.content,aid,'ok')
                      clearTimeout(dhtime)
                  }
                }else if(res.data.code == 501) {
                  this.notfound+=1
                  if(this.notfound >= 8) {
                    this.$emit("backHome",'【小猪】：'+'当前聊天人数过多，请稍后再试。',aid,'error')
                    clearTimeout(dhtime)
                    this.notfound = 0
                    return
                  }
                      this.timer(aid)
                }
          
            },
            fail: () => {
              this.$emit("backHome",'【小猪】：'+'当前聊天人数过多，请稍后再试。',aid,'error')
            },
            complete: () => {
            }
          });
        
        },1000) 
        
      }
    }
  }
</script>

<style lang="less">

</style>